﻿@using eLoan.Models;
@model CollectionReportModel

@{
    ViewBag.Title = "Collection Report";
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>Collection Report</h2>
        <ol class="breadcrumb">
            <li>
                Reports
            </li>
            <li class="active">
                <strong>Collection Report</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2"></div>
</div>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
            @using (Html.BeginForm("ExportCollectionToPdf", "Report", FormMethod.Post, new { id = "saveForm" }))
            {
                <fieldset>

                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true)

                <div class="ibox-title">
                    
                    <div id="divSearchRange" class="divSearch">
                        <table class="table-search">
                            <tr>
                                <td class="tdFixed">
                                    <span>@Html.LabelFor(m => m.Branches)</span>
                                    @Html.DropDownListFor(m => m.SelectedBranchCode, Model.Branches, new { @id = "ddlBranches", @class = "text-box single-line", @onchange = "GetAgents();" })
                                </td>
                                <td class="tdFixed">
                                    @Html.CheckBoxFor(m => m.IsAgentSelected, new { @id = "chkAgent", @onclick = "SetAgent();" })
                                    <span>@Html.LabelFor(m => m.Agents)</span>
                                    @Html.DropDownListFor(m => m.SelectedAgentID, Model.Agents, new { @id = "ddlAgents", @class = "text-box single-line" })
                                </td>
                                <td class="tdFixed">
                                    <span>@Html.LabelFor(m => m.PaymentTypes)</span>
                                    @Html.DropDownListFor(m => m.SelectedPaymentTypeID, Model.PaymentTypes, new { @id = "ddlLoanTypes", @class = "text-box single-line" })
                                </td>
                                <td class="tdFixed">
                                    @Html.LabelFor(m => m.FromDate)
                                    <div class="form-group" id="fd">
                                        <div class="input-group date">
                                            <span class="input-group-addon">
                                                <i class="fa fa-calendar"></i>
                                            </span>
                                            @Html.TextBoxFor(m => m.FromDate, "{0:MM/dd/yyyy}", new { @id = "txtFromDate", @class = "form-control" })
                                        </div>
                                    </div>
                                </td>
                                <td class="tdFixed">
                                    @Html.LabelFor(m => m.ToDate)
                                    <div class="form-group" id="td">
                                        <div class="input-group date">
                                            <span class="input-group-addon">
                                                <i class="fa fa-calendar"></i>
                                            </span>
                                            @Html.TextBoxFor(m => m.ToDate, "{0:MM/dd/yyyy}", new { @id = "txtToDate", @class = "form-control" })
                                        </div>
                                    </div>
                                </td>
                                <td class="tdFixed">
                                    <button type="submit" id="btnRefresh" class="btn btn-primary btnSearch" value="Refresh">Search</button>
                                </td>
                                <td class="tdAlignRight">
                                    <button type="submit" id="btnExportSummary" class="btn btn-primary btnSearch" name="command" value="ExportSummary">Export Summary to pdf</button>
                                </td>
                                <td class="tdAlignRight">
                                    <button type="submit" id="btnExport" class="btn btn-primary btnSearch" name="command" value="Export">Export to pdf</button>
                                </td>
                                <td class="tdAlignRight">
                                    <button type="submit" id="btnExportToExcel" class="btn btn-primary btnSearch" name="command" value="ExportToExcel">Export to excel</button>
                                </td>
                            </tr>
                        </table>
                    </div>
                    
                </div>
                <div class="ibox-title">
                    <div class="pull-right">
                        <h3>
                            Total Penalty: @Html.ValueFor(m => Model.TotalPenalty, "{0:#,###,##0.00}")
                            @Html.HiddenFor(m => Model.TotalPenalty)
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            Total Payment: @Html.ValueFor(m => Model.TotalPayment, "{0:#,###,##0.00}")
                            @Html.HiddenFor(m => Model.TotalPayment)
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            Total Collection: @Html.ValueFor(m => Model.TotalCollection, "{0:#,###,##0.00}")
                            @Html.HiddenFor(m => Model.TotalCollection)
                        </h3>
                    </div>
                </div>
                <div class="ibox-content">
                    <table class="table table-bordered table-List">
                        <thead>
                            <tr>
                                <th><span>Agent</span></th>
                                <th><span>Borrower</span></th>
                                <th><span>Payment Type</span></th>
                                <th><span>Interest Rate</span></th>
                                <th><span>Penalty</span></th>
                                <th><span>Loan Payment</span></th>
                            </tr>
                        </thead>
                        <tbody>
                            @for (var i = 0; i < Model.CollectionReportItems.Count(); i++)
                            { 
                                <tr>
                                    <td>
                                        @Html.HiddenFor(m => m.CollectionReportItems[i].AgentID)
                                        @Html.HiddenFor(m => m.CollectionReportItems[i].AgentName)
                                        @Html.ValueFor(m => m.CollectionReportItems[i].AgentName)
                                    </td>
                                    <td>
                                        @Html.ValueFor(m => m.CollectionReportItems[i].BorrowerName)
                                        @Html.HiddenFor(m => m.CollectionReportItems[i].BorrowerName)
                                    </td>
                                    <td>
                                        @Html.ValueFor(m => m.CollectionReportItems[i].PaymentTypeName)
                                        @Html.HiddenFor(m => m.CollectionReportItems[i].PaymentTypeName)
                                    </td>
                                    <td>
                                        @Html.ValueFor(m => m.CollectionReportItems[i].InterestRate)
                                        @Html.HiddenFor(m => m.CollectionReportItems[i].InterestRate)
                                    </td>
                                    <td style="text-align:right">
                                        @Html.ValueFor(m => m.CollectionReportItems[i].Penalty, "{0:#,###,##0.00}")
                                        @Html.HiddenFor(m => m.CollectionReportItems[i].Penalty)
                                    </td>
                                    <td style="text-align:right">
                                        @Html.ValueFor(m => m.CollectionReportItems[i].PostedPayment, "{0:#,###,##0.00}")
                                        @Html.HiddenFor(m => m.CollectionReportItems[i].PostedPayment)
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
                </fieldset>
            }
            </div>
        </div>
    </div>
</div>

@section Styles {
    @Styles.Render("~/Content/plugins/dataTables/dataTablesStyles")
    @Styles.Render("~/plugins/dataPickerStyles")

    <style>

    select[disabled], select[disabled]:hover {
        pointer-events: none;
        color: #e1e1e1;
    }

    .divScroll {
        height: 400px;
        overflow-x: scroll;
        border: solid 1px #EBEBEB;
    }

    .divSearch {
        background-color: #F5F5F6;
        border: 1px solid #EBEBEB;
        margin-top: 5px;
    }

    .table-search {
        width: 100%;
        border: none;
    }

        .table-search td {
            margin: 10px 10px 10px 10px;
            padding: 10px 10px 10px 10px;
            vertical-align: bottom;
        }
        
        .table-search .tdAlignRight {
            text-align: right;
        }

        .table-search .tdFixed {
            width:20%;
        }

        .table-search .tdFixed div {
            margin-bottom: 0px;
        }

            .table-search td .btnSearch {
                margin-bottom: 0px;
            }

            
            .table-search td span {
                margin-left: 7px;
            }
</style>

}

@section Scripts {
@Scripts.Render("~/plugins/dataPicker")
@Scripts.Render("~/plugins/dataTables")

    <script id="methods" type="text/javascript">

        $(document).ready(function () {
            
            $('.table-List').dataTable({
                searching: false,
                paging: false,
                ordering: true,
                scrollY: 400

            });

            $(window).keydown(function (event) {
                if (event.keyCode == 13) {
                    Refresh();
                    return false;
                }
            });

            SetDateFormat();
            SetAgent();
        });

        function SetDateFormat()
        {
            
            $('#fd .input-group.date').datepicker({
                todayBtn: "linked",
                keyboardNavigation: false,
                forceParse: false,
                calendarWeeks: true,
                format: 'mm/dd/yyyy',
                autoclose: true

            });

            $('#td .input-group.date').datepicker({
                todayBtn: "linked",
                keyboardNavigation: false,
                forceParse: false,
                calendarWeeks: true,
                format: 'mm/dd/yyyy',
                autoclose: true

            });

            var fd = $('#txtFromDate').val();
            $('#fd .input-group.date').datepicker("update", new Date(fd));

            var td = $('#txtToDate').val();
            $('#td .input-group.date').datepicker("update", new Date(td));

        }

        function GetAgents()
        {
            var val = $('#ddlBranches').val();
            var subItems = "";

            $.getJSON("@Url.Action("GetAgents","Borrower")", { branchCode: val },
            function (data) {

                $.each(data, function (index, item) {

                    subItems += "<option value='" + $.trim(item.Value) + "'>" + $.trim(item.Text) + "</option>"

                });

                $("#ddlAgents").html(subItems)
            });
        }

        function SetAgent()
        {
            var isChecked = $('#chkAgent').is(":checked");

            if (isChecked) {
                $('#ddlAgents').removeAttr('disabled');
            }
            else {
                $('#ddlAgents').attr('disabled', 'disabled');
            }
        }

        function Refresh() {
            document.getElementById('btnRefresh').click();
        }

    </script>
}